package com.cloudpolice.weixin.service.Impl;

import com.cloudpolice.weixin.dao.WechatMapper;
import com.cloudpolice.weixin.model.InPutParam.WechatInsertInParam;
import com.cloudpolice.weixin.model.InPutParam.WechatSelectInParam;
import com.cloudpolice.weixin.model.WechatModel;
import com.cloudpolice.weixin.service.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：梁昊，后端工程师：flyman5，前端工程师：孙耀
 * @create 2020-02-20 16:47
 * @function
 * @editLog
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class WechatServiceImpl implements WechatService {
    @Autowired
    WechatMapper wechatMapper;

    /**
     * 根据AppId、openId、Mobile，得到个人ID
     *
     * @param wechatInsertInParam 根据AppId、openId、Mobile，得到个人ID
     * @return 微信小程序信息
     */
    @Override
    public WechatInsertInParam SelectWeiXinInfoByMobile(WechatInsertInParam wechatInsertInParam) {
        return wechatMapper.SelectWeiXinInfoByMobile(wechatInsertInParam);
    }

    /**
     * 根据个人ID与人员类型，得到微信小程序信息
     *
     * @param wechatSelectInParam 根据个人ID与人员类型，得到微信小程序信息
     * @return 微信小程序信息
     */
    @Override
    public List<WechatModel> SelectWeiXinInfoByPersonId(WechatSelectInParam wechatSelectInParam) {
        return wechatMapper.SelectWeiXinInfoByPersonId(wechatSelectInParam);
    }

    @Override
    public int updateWechatPerson(WechatInsertInParam wechatInsertInParam) {
        return wechatMapper.updateWechatPerson(wechatInsertInParam);
    }

    /**
     * 根据个人ID与人员类型，得到微信小程序信息
     *
     * @param wechatInsertInParam com.cloudpolice.weixin.model.InPutParam.WechatInsertInParam
     * @return 微信小程序信息
     */
    @Override
    public int insertIntoWechat(WechatInsertInParam wechatInsertInParam) {
        int repetitionCount = wechatMapper.insertIntoWechatBeforeCheck(wechatInsertInParam);
        if (repetitionCount > 0) {
            boolean isUpdate = false;
            isUpdate = isUpdate || wechatInsertInParam.getNickName() != null ? true : false;
            isUpdate = isUpdate || wechatInsertInParam.getHeadPicture() != null ? true : false;
            isUpdate = isUpdate || wechatInsertInParam.getPersonWXCode() != null ? true : false;
            if (isUpdate)
                return wechatMapper.updateWechat(wechatInsertInParam);
            else
                return -1000;
        } else
            return wechatMapper.insertIntoWechat(wechatInsertInParam);
    }
}
